<?php
include_once(dirname(__FILE__) . "/service-adapter-base.php");
include_once(dirname(__FILE__) . "/util.php");
include_once(dirname(__FILE__) . "/account/account.php");

class CounterServiceAdapter extends ServiceAdapterBase {
  function __construct() {
    parent::__construct('127.0.0.1', '9094');
    $this->service_ = new GenericCounterClient($this->protocol_);
  }

  protected function LoadIDL() {
    require_once './gen-php/ServiceBase/ServiceBase.php';
    require_once './gen-php/ServiceBase/ServiceBase_types.php';
    require_once './gen-php/GenericCounter/GenericCounter.php';
    require_once './gen-php/GenericCounter/GenericCounter_types.php';
  }

  public function Post($article_id, $content) {
    $comment = new CommentItem();

    $comment->id = 0;
    $comment->state = 0;
    $comment->article_id = $article_id;
    # TODO: 如果是登录用户，则name字段无效
    $comment->sender_id = GetLoginId($_COOKIE['t']);
    $comment->sender_name = $_POST['name'];
    $comment->sender_ip = GetClientIp();
    $comment->time_stamp = time();
    $comment->content = $content;

    try {
      $this->service_->Post($comment);
    } catch (TException $tx) {
      return -1;
    }
    return 0;
  }

  public function GetArticleComments($article_id, $offset, $count) {
    try {
      return $this->service_->GetArticleComments($article_id, $offset, $count);
    } catch (TException $tx) {
      return null;
    }
    return null;
  }

  public function GetArticleCommentCount($article_id) {
    try {
      return $this->service_->GetArticleCommentCount($article_id);
    } catch (TException $tx) {
      return -1;
    }
    return -1;
  }

  public function SetAuditState($id, $audit_state) {
    try {
      $this->service_->SetAuditState($id, $audit_state);
    } catch (TException $tx) {
      return -1;
    }
    return 0;
  }

  public function GetComments($audit_state, $offset, $count) {
    try {
      return $this->service_->GetComments($audit_state, $offset, $count);
    } catch (TException $tx) {
      return null;
    }
    return null;
  }
}
?>
